﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AksesAPIMahasiswaHttpClient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetDataMahasiswa();
    }

    protected void GetDataMahasiswa()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:8492/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync("api/Mahasiswa").Result;
        if (response.IsSuccessStatusCode)
        {
            var data = response.Content.ReadAsAsync<IEnumerable<Mahasiswa>>().Result;
            GridView_Main.DataSource = data;
            GridView_Main.DataBind();
        }
        else
        {
            Label_Message.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
        }
    }
    protected void Button_Simpan_Click(object sender, EventArgs e)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:8492/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        Mahasiswa mhs = new Mahasiswa();
        mhs.NIM = TextBox_NIM.Text;
        mhs.Nama = TextBox_Nama.Text;
        mhs.Umur = Convert.ToInt32(TextBox_Umur.Text);
        mhs.Alamat = TextBox_Alamat.Text;
        mhs.Jurusan = TextBox_Jurusan.Text;

        HttpResponseMessage response = client.PostAsJsonAsync("api/Mahasiswa/", mhs).Result;
        if (response.IsSuccessStatusCode)
        {
            Label_Message.Text = "Data mahasiswa berhasil disimpan.";

            TextBox_NIM.Text = String.Empty;
            TextBox_Nama.Text = String.Empty;
            TextBox_Umur.Text = String.Empty;
            TextBox_Alamat.Text = String.Empty;
            TextBox_Jurusan.Text = String.Empty;

            GetDataMahasiswa();
        }
        else
        {
            Label_Message.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
        }
    }
    protected void GridView_Main_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = Convert.ToString(GridView_Main.SelectedValue);

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:8492/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync("api/Mahasiswa?nim=" + id).Result;
        if (response.IsSuccessStatusCode)
        {
            Mahasiswa item = response.Content.ReadAsAsync<Mahasiswa>().Result;

            TextBox_NIM.Text = item.NIM;
            TextBox_Nama.Text = item.Nama;
            TextBox_Umur.Text = item.Umur.ToString();
            TextBox_Alamat.Text = item.Alamat;
            TextBox_Jurusan.Text = item.Jurusan;

            Button_Update.Visible = true;
        }
        else
        {
            Label_Message.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
        }
    }
    protected void Button_Update_Click(object sender, EventArgs e)
    {
        string id = Convert.ToString(GridView_Main.SelectedValue);

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:8492/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        Mahasiswa mhs = new Mahasiswa();
        mhs.NIM = TextBox_NIM.Text;
        mhs.Nama = TextBox_Nama.Text;
        mhs.Umur = Convert.ToInt32(TextBox_Umur.Text);
        mhs.Alamat = TextBox_Alamat.Text;
        mhs.Jurusan = TextBox_Jurusan.Text;

        HttpResponseMessage response = client.PutAsJsonAsync("api/Mahasiswa?nim=" + id, mhs).Result;
        if (response.IsSuccessStatusCode)
        {
            Label_Message.Text = "Data mahasiswa berhasil diupdate.";

            TextBox_NIM.Text = String.Empty;
            TextBox_Nama.Text = String.Empty;
            TextBox_Umur.Text = String.Empty;
            TextBox_Alamat.Text = String.Empty;
            TextBox_Jurusan.Text = String.Empty;

            Button_Update.Visible = true;

            GetDataMahasiswa();
        }
        else
        {
            Label_Message.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
        }
    }
protected void GridView_Main_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = GridView_Main.DataKeys[e.RowIndex].Values["NIM"].ToString();

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:8492/");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.DeleteAsync("api/Mahasiswa?nim=" + id).Result;

    if (response.IsSuccessStatusCode)
    {
        GetDataMahasiswa();
    }
    else
    {
        Label_Message.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
    }
}
}